package com.zy.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.business.domain.TbAbsenceAudit;
import com.zy.business.domain.TbOverwork;
import com.zy.business.domain.TbOverworkAudit;
import com.zy.business.domain.vo.OverworkDto;
import com.zy.business.service.OverworkAuditService;
import com.zy.business.service.OverworkService;
import com.zy.common.annotation.Log;
import com.zy.common.annotation.RepeatSubmit;
import com.zy.common.core.controller.BaseController;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.core.page.TableDataInfo;
import com.zy.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 加班管理控制器
 * @author: whf
 * @create: 2023-02-08 10:39
 **/
@RestController
@Api(tags = "加班管理接口")
@RequestMapping("/overwork")
public class OverworkController extends BaseController {

    @Autowired
    private OverworkService overworkService;

    @Autowired
    private OverworkAuditService auditService;

    @ApiOperation("获取加班列表")
    @GetMapping("/list")
    public TableDataInfo list(OverworkDto overworkDto){
        startPage();
        List<TbOverwork> list = overworkService.list(overworkDto);
        return getDataTable(list);
    }

    @ApiOperation(value = "获取加班信息")
    @GetMapping("/getDetail")
    public AjaxResult getDetail(@RequestParam Long overworkId,Long approveId) {
        TbOverwork overwork = overworkService.getDetail(overworkId);
        if (approveId!=null){
            TbOverworkAudit audit = auditService.getOne(new QueryWrapper<TbOverworkAudit>().eq("overwork_id",overworkId).eq("audit_user_id",approveId));
            if (overwork==null || audit==null){
                return AjaxResult.error("该条加班申请已撤回");
            }
        }
        return AjaxResult.success(overwork);
    }

    @ApiOperation(value = "获取审核列表")
    @GetMapping("/getAuditList")
    public AjaxResult getAuditList(Long overworkId) {
        return AjaxResult.success(overworkService.getAuditList(overworkId));
    }

    @ApiOperation(value = "新增加班")
    @Log(title = "加班管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody TbOverwork overwork){
        overwork.setCreateBy(getUsername());
        return toAjax(overworkService.addOverwork(overwork));
    }

    @ApiOperation(value = "修改加班")
    @Log(title = "加班管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbOverwork overwork){
        return toAjax(overworkService.editOverwork(overwork));
    }

    @ApiOperation(value = "删除加班")
    @Log(title = "加班管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{overworkId}")
    public AjaxResult delete(@PathVariable Long overworkId){
        overworkService.removeById(overworkId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "加班审核")
    @Log(title = "加班管理", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    @RepeatSubmit
    public AjaxResult audit(@RequestBody TbOverworkAudit audit){
        return toAjax(overworkService.audit(audit));
    }

    @ApiOperation(value = "加班统计")
    @GetMapping("/getTjData")
    public TableDataInfo getDeptAttendance(){
        return overworkService.getTjData();
    }
}
